package com.g7.figmaapi.user.service.impl;

import com.g7.figmaapi.common.constant.CommonConstant;
import com.g7.figmaapi.user.dao.UserAuthorizationDao;
import com.g7.figmaapi.user.domain.bo.UserAuthorizationBo;
import com.g7.figmaapi.user.domain.entity.UserAuthorization;
import com.g7.figmaapi.user.mapper.UserAuthorizationMapper;
import com.g7.figmaapi.user.service.UserAuthorizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class UserAuthorizationServiceImpl implements UserAuthorizationService {
    @Resource
    private UserAuthorizationDao userAuthorizationDao;
    @Resource
    private UserAuthorizationMapper userAuthorizationMapper;

    @Override
    public int execVerifyUserAuthorizationBind(String channelType, String extId, Long currentUserId) {
        UserAuthorizationBo userAuthorizationBo = new UserAuthorizationBo();
        userAuthorizationBo.setExcludeUserId(currentUserId);
        userAuthorizationBo.setChannelType(channelType);
        userAuthorizationBo.setExtId(extId);
        UserAuthorization userAuthorization = userAuthorizationDao.getUserAuthorizationOneByParam(userAuthorizationBo);
        if (userAuthorization != null) {
            log.info("删除其他用户绑定::::{}", userAuthorization.toString());
            UserAuthorization userAuthorizationParam = new UserAuthorization();
            userAuthorizationParam.setId(userAuthorization.getId());
            userAuthorizationParam.setIsEnabled(CommonConstant.IS_NOT_ENABLED);
            userAuthorizationParam.setDataRemark("重复绑定解除当前用户绑定关系");
            return userAuthorizationMapper.updateById(userAuthorizationParam);
        }
        return 1;
    }
}
